package org.opens.web.config;

import org.opens.exception.BusinessException;
import org.opens.util.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    /**
     * 请求参数异常处理
     */
    @ExceptionHandler(BusinessException.class)
    public R<?> handleMethodArgumentNotValidException(BusinessException ex, HttpServletRequest request) {
       return R.error(null, ex.getMessage());
    }

    /**
     * 请求参数异常处理
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public R<?> handleIllegalArgumentException(IllegalArgumentException ex, HttpServletRequest request) {
        return R.error(null, ex.getMessage());
    }

}
